import SessionCache from "@/utils/cache";
/* eslint-disable no-mixed-spaces-and-tabs */
import Vue from "vue";
import VueRouter from "vue-router";

import Home from "../views/Home.vue";

/**
 * 防止在当前路由再次点击报错
 */
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [{
		path: "/",
		redirect: "/Content",
		component: Home,

		children: [{
				path: "/Content",
				name: "Content",
				component: () => import("../views/content"),
			},
			{
				path: "/informs",
				name: "informs",
				component: () => import("../views/inform/informs"),
			},
			{
				path: "/informMsg",
				name: "informMsg",
				component: () => import("../views/inform/informMsg"),
			},
			{
				path: "/policy",
				name: "policy",
				component: () => import("../views/policy/policy"),
			},
			{
				path: "/policyItems",
				name: "policyItems",
				component: () => import("../views/policy/policyItems"),
			},
			{
				path: "/newsInforms",
				name: "newsInforms",
				component: () => import("../views/newsOrNotice/newsInforms"),
			},
			{
				path: "/introductions",
				name: "introductions",
				component: () => import("../views/introduction/introductions"),
			},
			{
				path: "/major",
				name: "major",
				component: () => import("../views/major/major"),
			},
			{
				path: "/newsMsg",
				name: "newsMsg",
				component: () => import("../views/newsOrNotice/newsMsg"),
			},
			{
				path: "/help",
				name: "help",
				component: () => import("../views/learning/learning-help"),
			},
		],
	},
	{
		path: "/studyHome",
		name: "studyHome",
		redirect: "/studyHome/MyCourse",
		component: () => import("../views/study/studyHome"),
		children: [{
				path: "/MyCourse",
				name: "MyCourse",
				component: () => import("../views/study/studyinfo/MyCourse"),
			},
			{
				path: "/inquire_grade",
				name: "inquire_grade",
				component: () => import("../views/study/studyinfo/inquire_grade"),
			},
			{
				path: "/pwd_manage",
				name: "pwd_manage",
				component: () => import("../views/study/studyinfo/pwd_manage"),
			},
			{
				path: "/problem_feedback",
				name: "problem_feedback",
				component: () => import("../views/study/studyinfo/problem_feedback"),
			},
			{
				path: "/per_data",
				name: "per_data",
				component: () => import("../views/study/studyinfo/per_data"),
			},
		],
	},
	{
		path: "/CourseStudy",
		name: "CourseStudy",
		component: () => import("../views/course/CourseStudy"),
		children: [{
				path: "/courseTask",
				name: "courseTask",
				component: () => import("../views/course/courseInfo/courseTask"),
			},
			{
				path: "/chapter",
				name: "chapter",
				component: () => import("../views/course/courseInfo/chapter"),
			},
			{
				path: "/detial",
				name: "detial",
				component: () => import("../views/course/courseInfo/detial"),
			},
			{
				path: "/homework",
				name: "homework",
				component: () => import("../views/course/courseInfo/homework"),
			},
			{
				path: "/exam",
				name: "exam",
				component: () => import("../views/course/courseInfo/exam"),
			},
		],
	},
	{
		path: "/examPerson",
		name: "examPerson",
		component: () => import("../views/exam/examPerson"),
	},
	{
		path: "/examing",
		name: "examing",
		component: () => import("../views/exam/examing"),
	},
	{
		path: "/examList",
		name: "examList",
		component: () => import("../views/exam/examList"),
		children: [{
				path: "/examListContent",
				name: "examListContent",
				component: () => import("../views/exam/examListContent"),
			},
			{
				path: "/examFace",
				name: "examFace",
				component: () => import("../views/exam/examFace"),
			},
		],
	},
	{
		path: "/videos",
		name: "videos",
		component: () => import("../views/course/video/videos"),
		children: [{
				path: "/videoplear",
				name: "videoplear",
				// component: () => import("../views/course/video/videoplear"),
			},
			{
				path: "/examTest",
				name: "examTest",
				component: () => import("../views/course/video/examTest"),
			},
		],
	},
	{
		path: "/login",
		name: "login",
		component: () => import("../views/login/login.vue"),
	},
];

const router = new VueRouter({
	mode: "hash",
	routes,
});

const ignorePage = [
	"/Content",
	"/policyItems",
	"/login",
	"/policy",
	"/newsInforms",
	"/newsMsg",
	"/help",
	"/introductions",
	"/informMsg",
	"/informs",
	"/major",
	"/examPerson"
];

router.beforeEach((to, from, next) => {
	if (ignorePage.includes(to.path)) {
		next();
	} else {
		const token = SessionCache.getCache("IDInfo");
		token ? next() : next("/login")	
	}
});
export default router;
